package subumbo.micro.controller
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import subumbo.micro.model.MicroVO;
	
	public class ListViewController extends Component
	{
		protected var _container:Sprite;
		
		/**
		 * 
		 * @param e
		 * 
		 */		
		
		override protected function init(e:Event):void
		{
			_container = new Sprite();
			this.addChild(_container);
			super.init(e);	
		}
		
		protected function select(index:int):void
		{
			var i:int = _container.numChildren;
			var item:Listable;
			while( --i > - 1 )
			{
				item = Listable(_container.getChildAt(i));
				if(item.index == index)
				{
					item.selected = true;
				}else
				{
					item.selected = false;
				}
			}
		}
		
		protected function build( data:Array, libraryItemName:String, gap:Number=10, vertical:Boolean=false ):void
		{
			var l:int = data.length;
			var i:int;
			
			var item:*;				
			
			if(!vertical)
			{
				for(; i < l; ++i)
				{
					item = ClassExtractor.extractMC(libraryItemName);
					_container.addChild(item);
					item.data = data[i];
					item.x = i * (item.width + gap);
				}
			}else
			{
				for(; i < l; ++i)
				{
					item = ClassExtractor.extractMC(libraryItemName);
					_container.addChild(item);
					item.data = data[i];
					item.y = i * (item.height + gap);
				}
			}
		}
		
		override public function update(e:ApplicationEvent):void
		{	
			if(e.data is MicroVO)
			select(e.data.index);	
		}
	}
}